// pages/marketCenter2/requireGoods/requireGoods.js
const app = getApp()
import api from '../../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //等待框
    waitHidden: true,
    goodsItemList: [],
    navBarHeight: 88, //导航栏高度
    menuBotton: app.globalData.menuBotton, //导航栏距离顶部距离
    menuHeight: app.globalData.menuHeight, //导航栏高度
    //页面尺寸数据
    windowHeight: 0,
    screenHeight: 0,
    height: 1624,
    pageIndex: 1,
    pageSize: 10,
    userId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.hideShareMenu()
    //设置屏幕尺寸数据
    that.setData({
      windowHeight: app.globalData.windowHeight,
      screenHeight: app.globalData.screenHeight,
      height: that.data.screenHeight,
      userId: wx.getStorageSync('logindata').data.id
    })

    that.setData({
      waitHidden:false
    })
    that.requestGoods()
    setTimeout(function () {
      that.setData({
        waitHidden:true
      })
    }, 1000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    that.setData({
      pageIndex: that.data.pageIndex + 1,
      waitHidden: false
    })
    if (!that.data.isLastPage) {
      // 我求购的商品列表查询
      that.requestGoods()
    }
    that.setData({
      waitHidden: true
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  goBack: function (e) {
    wx.navigateBack({
      delta: 1,
    })
  },

  // 请求我求购的商品
  requestGoods: function () {
    var that = this
    var goodsItemListtemp = []

    wx.request({
      url: api.getMyPurchaseInfoList,
      data: {
        pageIndex: that.data.pageIndex,
        pageSize: that.data.pageSize,
        buyerId: wx.getStorageSync('logindata').data.id
      },
      method: "POST",
      success(res) {
        if (res.data.data.records.length != 0) {
          for (var i = 0; i < res.data.data.records.length; i++) {
            var index = i
            var title = res.data.data.records[i].title
            var bottomPrice = res.data.data.records[i].bottomPrice
            var peakPrice = res.data.data.records[i].peakPrice
            var imgUrl = res.data.data.records[i].imgUrl
            var status = res.data.data.records[i].status
            var name = res.data.data.records[i].sellerName
            var id = res.data.data.records[i].orderId?res.data.data.records[i].orderId:res.data.data.records[i].PurchaseInfoId
            var tmp = {
              index,
              title,
              bottomPrice,
              peakPrice,
              imgUrl,
              status,
              name,
              id
            }
            goodsItemListtemp.push(tmp)
          }

          var heightAddTotal = 0
          var heightAdd = 0
          for (var i = 0; i < goodsItemListtemp.length; i++) {
            if (goodsItemListtemp[i].status == "1" || goodsItemListtemp[i].status == "2") {
              heightAdd = 400
            } else if (goodsItemListtemp[i].status == "3" || goodsItemListtemp[i].status == "4") {
              heightAdd = 300
            }
            heightAddTotal = heightAddTotal + heightAdd
            that.data.goodsItemList.push(goodsItemListtemp[i])
          }
          that.setData({
            goodsItemList: that.data.goodsItemList,
            isLastPage: res.data.data.pageIndex >= res.data.data.totalPages
          })

          if (that.data.goodsItemList.length > 3) {
            if(res.data.data.pageIndex == 1) {
              wx.createSelectorQuery().select('#item0 .view').boundingClientRect((rect) => {
                that.setData({
                  height: heightAddTotal + 100
                })
              }).exec()
            } else {
              wx.createSelectorQuery().select('#item0 .view').boundingClientRect((rect) => {
                that.setData({
                  height: that.data.height + heightAddTotal + 100
                })
              }).exec()
            }
          } else {
            that.setData({
              height: that.data.screenHeight
            })
          }
        }else {
          that.setData({
            isLastPage: true
          })
        }
      }
    })
  },

  // 【我的求购】-取消交易
  cancelDeal: function (e) {
    var that = this
    wx.request({
      url: api.cancelPurchaseDeal,
      method: "POST",
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      data: {
        Order_id: that.data.goodsItemList[e.currentTarget.dataset.index].id, 
        User_id: that.data.userId
      },
      success(res) {
        if(res.data.state != 1){
          wx.showToast({
            title: '操作失败',
            // title: res.data.data,
            icon: 'error',
          })
        }
      }
    })
    setTimeout(function() {
      //刷新列表
      that.setData({
        goodsItemList: new Array,
        pageIndex: 1,
        height:that.data.screenHeight
      })
      that.requestGoods()
    })
  },



//【我的求购】-取消求购
  btnCancelPurchase: function (e) {
    var that = this
    wx.request({
      url: api.btnCancelPurchaseGoods,
      method: "POST",
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      data: {
        id: that.data.goodsItemList[e.currentTarget.dataset.index].id,
        status: 4
      },
      success(res) {
        if(res.data.result != 1){
          wx.showToast({
            title: res.data.msg,
            icon: 'error',
          })
        }
      }
    })
    setTimeout(function() {
      //刷新列表
      that.setData({
        goodsItemList: new Array,
        pageIndex: 1,
        height:that.data.screenHeight
      })
      that.requestGoods()
    },300)
  },

//【我的求购】- 确认收货
  btnConfirmPurchase: function(e) {
    var that = this
    wx.request({
      url: api.purchaseMakeDeal,
      method: "POST",
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      data: {
        order_id: that.data.goodsItemList[e.currentTarget.dataset.index].id
      },
      success(res) {
        if(res.data.result != "1"){
          wx.showToast({
            title: res.data.msg,
            icon: 'error',
          })
        }
      }
    })
    setTimeout(function() {
      //刷新列表
      that.setData({
        goodsItemList: new Array,
        pageIndex: 1,
        height:that.data.screenHeight
      })
      that.requestGoods()
    },500)
  }
})